package store

import (
	"fmt"
	"sort"

	"file_explorer/common/consts"
	"file_explorer/model"

	"fyne.io/fyne/v2"
	"fyne.io/fyne/v2/theme"
)

type SearchResTabContext struct {
	DefaultTabContext

	Keyword     string
	FileInfos   []*model.FileInfo
	CheckedList []*model.FileInfo

	sortFunc func(a, b *model.FileInfo) bool
}

func (ctx *SearchResTabContext) GetTabType() string {
	return consts.TabTypeSearchRes
}

func (ctx *SearchResTabContext) GetTabLabel() string {
	return fmt.Sprintf("%s: %s", ctx.GetTabType(), ctx.Keyword)
}

func (ctx *SearchResTabContext) GetTabIcon() fyne.Resource {
	return theme.SearchIcon()
}

func (ctx *SearchResTabContext) SetSortFunc(cmp func(a, b *model.FileInfo) bool) {
	ctx.sortFunc = cmp
	sort.Slice(ctx.FileInfos, func(i, j int) bool {
		return ctx.sortFunc(ctx.FileInfos[i], ctx.FileInfos[j])
	})
}

func NewSearchResTabContext(gCtx *GlobalContext, keyword string, fileInfos []*model.FileInfo) *SearchResTabContext {
	ctx := &SearchResTabContext{
		DefaultTabContext: newDefaultTabContext(gCtx),
		Keyword:           keyword,
		FileInfos:         fileInfos,
	}

	return ctx
}
